<template>
  <div>
    <i-cell-group>
      <i-cell>
        <p slot="icon" class="left">
          <span>启用</span>
        </p>
        <p slot="footer">
          <i-switch :i-class="onOff ?'switch-background-color' : ''" :value="onOff" @change="onChange" slot="footer"></i-switch>
        </p>
      </i-cell>
      <i-cell
        v-for="(item,index) in list"
        :key="index"
        is-link
        @click="linkClick(item)"
      >
        <p slot="icon" class="left">
          <span>{{item.label}}</span>
        </p>
        <p slot="footer">{{form[item.key]}}</p>
      </i-cell>
    </i-cell-group>
  </div>
</template>

<script>
import { FIND_APPLY_INFO_API, SHOP_ADMIN_UPDATE } from '@/api'

export default {
  components: {},
  props: {},
  data () {
    return {
      onOff: false,
      form: {},
      list: [
        {
          label: '登录名',
          key: 'username',
          isUpdateText: false,
          isUpdataGW: false,
          isUpdatePass: false
        },
        {
          label: '昵称',
          key: 'nickName',
          isUpdateText: true,
          isUpdataGW: false,
          isUpdatePass: false
        },
        {
          label: '岗位',
          key: 'roleName',
          isUpdateText: false,
          isUpdataGW: true,
          isUpdatePass: false
        },
        {
          label: '修改密码',
          key: '',
          isUpdateText: false,
          isUpdataGW: false,
          isUpdatePass: true
        }
      ]
    }
  },
  computed: {
    account () {
      return this.$store.state.account
    }
  },
  watch: {},
  created () {},
  mounted () {},
  onShow () {
    this.getAdminInfo()
  },
  methods: {
    onChange (e) {
      const vm = this
      let value = e.mp.detail.value
      // to do 感觉用不上这句
      // vm.onOff = value
      let params = { ...vm.form }
      if (value === true) {
        params.state = 'NORMAL'
      } else {
        params.state = 'LOCKED'
      }
      SHOP_ADMIN_UPDATE(params)
        .then(r => {
          if (r.data.code === '0' && r.data.data) {
            vm.getAdminInfo()
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    },
    linkClick (item) {
      const vm = this
      if (item.isUpdateText) {
        console.log(vm.form)
        vm.$router.push(
          `/pages/mine/admin-manage/admin/adminUpdate/main?item=${JSON.stringify(
            vm.form
          )}`
        )
      } else if (item.isUpdataGW) {
        // vm.$router.push(
        //   `/pages/mine/admin-manage/admin/adminUpdate/main?item=${vm.form}`
        // )
      } else if (item.isUpdatePass) {
        vm.$router.push(
          `/pages/mine/admin-manage/admin/adminUpdatePassworder/main?item=${
            vm.form
          }`
        )
      } else {
        // 没事了
      }
    },
    getAdminInfo () {
      const vm = this
      FIND_APPLY_INFO_API(this.$root.$mp.query.adminId)
        .then(r => {
          if (r.data.code === '0') {
            vm.form = r.data.data
            if (vm.form.state === 'NORMAL') {
              vm.onOff = true
            } else {
              vm.onOff = false
            }
            let lis = []
            r.data.data.shopRoles.forEach(e => {
              lis.push(e.roleName)
            })
            vm.form.roleName = lis.join()
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import 'static/styles/color.scss';
</style>
